#!/usr/bin/perl

use strict;
use warnings;
use ConstantsJP;


my @o1 = qw/q101 q102 q103 q104 q105 q106 q107 q108 q109 q110 q201 q202 q203 q204 q206 q207 q208 q209 q210 q211 q212 q301 q302 q303 q304 q305 q306 q401 q402 q403 q404 q405 q406 q407 q408 q409 q410 q411 q412 q413 q414 q415 q416 q417 q418 q419 q420 q421 q422 q423 q424 q425 q426 q427 q428/;
my @o2 = qw/q403 q409/;
my @want = @o1;
my @p1_1 = ( {q401 => "==1", q409 => "==5",},);#SNS总体,真实用户最多
my @p1_2 = ( {q401 => "==1", q409 => "==4",},);
my @p1_3 = ( {q401 => "==1", q409 => "==3",},);
my @p1_4 = ( {q401 => "==1", q409 => "==2",},);
my @p1_5 = ( {q401 => "==1", q409 => "==1",},);#SNS总体,真实用户最少

my @q1_1 = ( {q401 => "==1", q403 => "==1", q409 => "==5",},);                  #SNS=QQ校友,真实用户最多
my @q1_2 = ( {q401 => "==1"}, {q403 => "==1"}, {q409 => "<5"}, {q409 => ">1"},);#SNS=QQ校友,真实用户最多
my @q1_3 = ( {q401 => "==1", q403 => "==1", q409 => "==1",},);                  #SNS=QQ校友,真实用户最少

my @q2_1 = ( {q401 => "==1", q403 => "==2", q409 => "==5",},);                  #SNS=开心,真实用户最多
my @q2_2 = ( {q401 => "==1"}, {q403 => "==2"}, {q409 => "<5"}, {q409 => ">1"},);#SNS=开心,真实用户最多
my @q2_3 = ( {q401 => "==1", q403 => "==2", q409 => "==1",},);                  #SNS=开心,真实用户最少

my @q3_1 = ( {q401 => "==1", q403 => "==3", q409 => "==5",},);                  #SNS=人人,真实用户最多
my @q3_2 = ( {q401 => "==1"}, {q403 => "==3"}, {q409 => "<5"}, {q409 => ">1"},);#SNS=人人,真实用户最多
my @q3_3 = ( {q401 => "==1", q403 => "==3", q409 => "==1",},);                  #SNS=人人,真实用户最少

my @p2 = ( {q401 => "==3",}, );
my @p3 = ( {q401 => "==2",}, );

my @exp = @q3_3;
my %summary;
my %summary_all;
my %id_count;

while (my $line=<>) {
    chomp($line);
    my @info = split (",",$line);
    next if ($info[0] eq "aipid");

#	next unless (filter(\@info));  #filter

	foreach my $column (@want) {
		get_answer($column,\@info);
	}
}
foreach my $q (@want) {
	next unless($id_count{$q}{member_count});
	printf "%s,%s\n",uc($q), $ex->{$q}->{text};
    print "------------------------------------\n";
	printf ("User Count:%s\n", $id_count{$q}{member_count}||0);
#    foreach my $key (sort keys %{$summary{$q}}) {
    foreach my $key (sort{$a<=>$b} keys %{$summary{$q}}) {
        printf "\t%s:\t%s\t%.2f%%\t%s\t%.2f%%\n",
			$ex->{$q}->{value}[$key],
			$summary{$q}{$key},
			$summary{$q}{$key}/$id_count{$q}{member_count}*100,
			$summary_all{$q}{$key},
			$summary{$q}{$key}/$summary_all{$q}{$key}*100;
    }
	print "\n";
}


########################
# lib function
########################
sub filter
{
	my $info = shift;

	foreach my $q (@exp) {
		foreach my $key (keys %$q) {
			if (eval(@$info[$ex->{$key}->{field}].$q->{$key})) {
				next;
			} else {
				return 0;
			}
		}
	}
	return 1;
}

sub get_answer
{
	my $key   = shift;
	my $info  = shift;
	my $index = $ex->{$key}->{field} || die '[$get_answer]';
	my @para = split ("_" , $index);
	$para[1] = 0 unless($para[1]);

	if (defined $ex->{$key}->{conditions}) {
		foreach my $cond (@{$ex->{$key}->{conditions}}) {
			my $k = (keys %$cond)[0];
			if (eval(@$info[$ex->{$k}->{field}].$cond->{$k})) {
				next;
			} else {
				return 0;
			}
		}
	}
	if ($ex->{$key}->{type} =~ /MA/) {
#		get_multi_answer($key,$info,@para);
		get_ma_ex($key,$info,@para);
	} elsif ($ex->{$key}->{type} =~ /SA/) {
#		get_single_answer($key,@$info[$para[0]],$para[1]);
		get_sa_ex($key,$info,@para);
	}
	return 1;
}
sub get_sa_ex
{
	my $key  = shift;
	my $info = shift;
    my $para = shift;
    my $ext  = shift || 0;

	my $count = $#{$ex->{$key}->{value}};
	my $value = @$info[$para];
	my $qualified = filter($info);
    my $text  = '';
    $text = sprintf "\t$ex->{$key}->{text}:";
    if (!$ext || ($value =~/^\d+$/ && $value < $count)) {
        $text .= sprintf "%s\n", $ex->{$key}->{value}[$value];
        $summary{$key}{$value}++ if ($qualified);
        $summary_all{$key}{$value}++;
    } else {
        $text .= sprintf "%s\n", $value;
        $summary{$key}{$count}++ if ($qualified);
        $summary_all{$key}{$count}++;
    }
	$id_count{$key}{member_count}++ if ($qualified);
}

sub get_single_answer
{
    my $key  = shift;
    my $para = shift;
    my $ext  = shift || 0;

	my $count = $#{$ex->{$key}->{value}};
    my $text  = '';
    $text = sprintf "\t$ex->{$key}->{text}:";
    if (!$ext || ($para =~/^\d+$/ && $para < $count)) {
        $text .= sprintf "%s\n", $ex->{$key}->{value}[$para];
        $summary{$key}{$para}++;
    } else {
        $text .= sprintf "%s\n", $para;
        $summary{$key}{$count}++;
    }
	$id_count{$key}{member_count}++;
}
sub get_ma_ex
{
    my $key  = shift;
    my $info = shift;
    my $para = shift;
    my $ext  = shift || 0;
    my $print= shift || 0;
    my $temp = '';
    my @answer; 
	my $count = $#{$ex->{$key}->{value}};

	my $qualified = filter($info);

    for (my $i = 0; $i < $count; $i++) {
        push @answer,$i+1 if (@$info[$para+$i]); 
    }
    $temp = sprintf "\t$ex->{$key}->{text}:";
    foreach (@answer) {
        if ($_ =~/^\d+$/ && $_ <= $count) {
            $summary{$key}{$_}++ if ($qualified);
            $summary_all{$key}{$_}++;
            $temp .= sprintf "%s,", $ex->{$key}->{value}[$_];
        } else {
            $summary{$key}{$count}++ if ($qualified);
            $summary_all{$key}{$count}++;
            $temp .= sprintf "%s,", $_;
        }
    }
    chop($temp);
    print $temp."\n" if ($print);
	$id_count{$key}{member_count}++ if($qualified);
}
sub get_multi_answer
{
    my $key  = shift;
    my $info = shift;
    my $para = shift;
    my $ext  = shift || 0;
    my $print= shift || 0;
    my $temp = '';
    my @answer; 
	my $count = $#{$ex->{$key}->{value}};
    for (my $i = 0; $i < $count; $i++) {
        push @answer,$i+1 if (@$info[$para+$i]); 
    }
    $temp = sprintf "\t$ex->{$key}->{text}:";
    foreach (@answer) {
        if ($_ =~/^\d+$/ && $_ <= $count) {
            $summary{$key}{$_}++;
            $temp .= sprintf "%s,", $ex->{$key}->{value}[$_];
        } else {
            $summary{$key}{$count}++;
            $temp .= sprintf "%s,", $_;
        }
    }
    chop($temp);
    print $temp."\n" if ($print);
	$id_count{$key}{member_count}++;
}
